package com.example.comsys.control;

import com.example.comsys.model.Employee;
import com.example.comsys.model.Page;
import com.example.comsys.model.Result;
import com.example.comsys.param.EmployeeQueryParam;
import com.example.comsys.service.DepartmentService;
import com.example.comsys.service.EmployeeService;
import com.example.comsys.util.ComSysConstant;
import com.example.comsys.util.ResultUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * employee api
 *
 * @author wjh
 * @date 2020/1/1, 周三
 */
@RestController
@RequestMapping(ComSysConstant.API_PREFIX + "/employee")
public class EmployeeController {

  @Autowired
  private EmployeeService employeeService;

  @Autowired
  private DepartmentService departmentService;

  @RequestMapping(value = "/list.json", method = {RequestMethod.GET, RequestMethod.POST})
  public Result<Page<Employee>> queryEmployees(@RequestBody(required = false) EmployeeQueryParam queryParam,
      @RequestParam(value = "limit", defaultValue = "true") boolean limit) {

    if (queryParam == null) {
      queryParam = new EmployeeQueryParam();
    }
    queryParam.setDetailDepart(true);
    queryParam.setLimit(limit);

    Page<Employee> data = employeeService.queryDataByParam(queryParam);
    return ResultUtils.newSuccessfulResult(data);
  }

  @PostMapping("add.do")
  public Result<Employee> addEmployee(@RequestBody Employee employee) {

    if (employee.getDepartment() == null || employee.getDepartment().getDepartId() <= 0) {
      return ResultUtils.newFailedResult("缺少必要参数");
    }

    Employee temp = employeeService.addEmployee(employee);
    return temp == null
        ? ResultUtils.newFailedResult("新增失败")
        : ResultUtils.newSuccessfulResult(temp);
  }


  @RequestMapping(value = "export.do", method = {RequestMethod.GET, RequestMethod.POST})
  public void exportExcel(@RequestParam(value = "filename", defaultValue = "employee") String filename,
      // @RequestBody Map<String, String> args,
      HttpServletResponse response) throws IOException {

    HSSFWorkbook workbook = employeeService.generateExcel(filename);
    OutputStream ops = response.getOutputStream();
    response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");
    response.setContentType("application/octet-stream");
    workbook.write(ops);

  }

  @PostMapping("update.do")
  public Result<Employee> updateEmployee(@RequestBody Employee employee) {

    if (employee.getEmployeeId() <= 0) {
      return ResultUtils.newFailedResult("缺少必要参数");
    }

    Employee temp = employeeService.updateEmployeeByEmployeeId(employee);
    return temp == null
        ? ResultUtils.newFailedResult("更新失败")
        : ResultUtils.newSuccessfulResult(temp);
  }

  @PostMapping("delete.action")
  public Result<Boolean> deleteEmployee(@RequestBody Map<String, String> args) {

    String value = args.get("employeeId");
    long employeeId = Long.parseLong(value);
    if (employeeId <= 0) {
      return ResultUtils.newFailedResult("缺少必要参数");
    }
    boolean b = employeeService.deleteEmployee(employeeId);

    return b ? ResultUtils.newSuccessfulResult(true) : ResultUtils.newFailedResult("删除失败");
  }

  // @GetMapping("genChart/employeeDistribution.action")
  // public Result<EmployeeDistribution> employeeDistributionChart() {
  //
  // }
}
